<?php
/**********************功能性工具******************************/
/**
 * @Desc     随机生成16位唯一编号
 * @Author   pekey
 * @DateTime 2020-05-12T19:29:04+0800
 * @return   [type]
 */
function buildSID()
{
    // return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    $trade_no = substr(date('Y'), 3, 1) . time() . rand(100, 999) . substr(implode(null, array_map('ord', str_split(substr(uniqid(), 5, 13), 1))), 9, 2);
    return $trade_no;
}

/**
 * [listToTree description]
 * @Description 根据pid无线级分类转化成树状多维数组
 * @Author      FengPQ
 * @DateTime    2020-05-14T08:37:38+0800
 * @param       [type]                   $list  [数据列]
 * @param       string                   $pk    [description]
 * @param       string                   $pid   [description]
 * @param       string                   $child [description]
 * @param       integer                  $root  [description]
 * @return      [type]                          [description]
 */
function listToTree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
{
    $tree = array(); // 创建Tree
    if (is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] = &$list[$key];
        }

        foreach ($list as $key => $data) {
            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[$data[$pk]] = &$list[$key];
            } else {
                if (isset($refer[$parentId])) {
                    $parent           = &$refer[$parentId];
                    $parent[$child][] = &$list[$key];
                }
            }
        }
    }
    return $tree;
}

/**
 * [getPasswrodWithTwiceEncode description]
 * @Description 二次加盐
 * @Author      FengPQ
 * @DateTime    2020-05-14T08:38:30+0800
 * @param       [type]                   $password [密码]
 * @param       [type]                   $salt     [盐]
 * @return      [type]                             [加密后的密码]
 */
function getPasswrodWithTwiceEncode($password, $salt)
{
    $encryptedPassword         = md5($password);
    $encryptedWithSaltPassword = md5($encryptedPassword . $salt);
    return $encryptedWithSaltPassword;
}

/**
 * [getRandNum description]
 * @Description 随机获取数字
 * @Author      FengPQ
 * @DateTime    2020-05-14T08:38:57+0800
 * @param       integer                  $length [description]
 * @return      [type]                           [description]
 */
function getRandNum($length = 8)
{
    // 密码字符集，可任意添加你需要的字符
    $chars = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

    // 在 $chars 中随机取 $length 个数组元素键名
    $keys     = array_rand($chars, $length);
    $password = '';
    for ($i = 0; $i < $length; $i++) {
        // 将 $length 个数组元素连接成字符串
        $password .= $chars[$keys[$i]];
    }
    return $password;
}

/**
 * [getRandStr description]
 * @Description 随机获取数字+字符串
 * @Author      FengPQ
 * @DateTime    2020-05-14T08:39:08+0800
 * @param       integer                  $length [字符串长度]
 * @return      [type]                           [description]
 */
function getRandStr($length = 8)
{
    // 密码字符集，可任意添加你需要的字符
    $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
        'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
        't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
        'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
        'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

    // 在 $chars 中随机取 $length 个数组元素键名
    $keys     = array_rand($chars, $length);
    $password = '';
    for ($i = 0; $i < $length; $i++) {
        // 将 $length 个数组元素连接成字符串
        $password .= $chars[$keys[$i]];
    }
    return $password;
}
